package com.csdj.server.util;

import com.csdj.server.pojo.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author zyq
 * @Date 2023/2/16 9:36
 * @ClassName: FastDFSClient
 * @Description: 文件操作的工具类
 * @Version 1.0
 */
public class FastDFSClient {

    private static Logger logger= LoggerFactory.getLogger(FastDFSClient.class);

    /**
     * 加载 配置文件
     */
    static {
        try {
            //获取配置文件路径
            String path = new ClassPathResource("fdfs_client.conf").getFile()
                    .getAbsolutePath();
            //全局加载 配置文件 根据路径
            ClientGlobal.init(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 跟踪器 tracker服务端
     * @return
     * @throws IOException
     */
    private static TrackerServer getTrackerServer() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getTrackerServer();
        return trackerServer;
    }

    /**
     * 获取到 存储 角色 storage
     * @return
     * @throws IOException
     */
    private static StorageClient getStorageClient() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = new StorageClient(trackerServer,
                null);
        return storageClient;
    }

    /**
     * 文件上传操作
     * @param file
     * @return
     */
    public static String[] upload(FastDFSFile file){
        logger.info("File Name:"+file.getName()+
                ",file length: "+file.getContent().length);
        //存放 文件的基础信息
        NameValuePair[] nameValuePair = new NameValuePair[1];
        nameValuePair[0]=new NameValuePair("author",file.getAuthor());
        StorageClient storageClient=null;
        String[] result=null;
        try {
            storageClient= getStorageClient(); //fdfs_upload_file
            result = storageClient.upload_file(file.getContent(),
                    file.getExt(), nameValuePair);
        } catch (Exception e) {
          logger.error("文件上传失败",e);
        }
        if(result==null&&storageClient!=null){
            logger.error("文件上传失败");
        }
        // group1/M00/00
        logger.info("文件上传路径为: "+result[0]+result[1]);
        return result;
    }
    // group1/M00

    /**
     * 文件的下载 操作
     * @param groupName
     * @param remoteFilePath
     * @return
     */
    public static InputStream downFile(String groupName,String remoteFilePath){

        try {
            StorageClient storageClient = getStorageClient();
            byte[] fileBytes = storageClient.download_file(groupName, remoteFilePath);
            InputStream ins= new ByteArrayInputStream(fileBytes);
            logger.info("文件下载成功!");
            return ins;
        } catch (IOException e) {
           logger.error("下载失败",e);
        } catch (Exception e) {
            logger.error("下载失败",e);
        }
        return null;
    }

    /**
     * 删除文件
     * @param groupName
     * @param remoteFilePath
     */
    public static void deleteFile(String groupName,String remoteFilePath){

        try {
            StorageClient storageClient = getStorageClient();
            int i = storageClient.delete_file(groupName, remoteFilePath);
            logger.info("删除文件成功"+i);
        } catch (IOException e) {
           logger.error("删除文件失败",e);
        } catch (Exception e) {
            logger.error("删除文件失败",e);
        }

    }

    /**
     * 获取文件 信息
     * @param groupName
     * @param remotePath
     * @return
     */
    public static FileInfo getFileInfo(String groupName,String remotePath){

        StorageClient storageClient = null;
        try {
            storageClient = getStorageClient();
            return   storageClient.get_file_info(groupName,remotePath);
        } catch (IOException e) {
           logger.error("获取文件信息失败",e);
        } catch (Exception e) {
            logger.error("获取文件信息失败",e);
        }
        return null;
    }

    /**
     * 获取文件上传 web地址
     * @return
     */
    public static String getTrackServerPath(){
        TrackerClient trackerClient = new TrackerClient();
        StorageServer storeStorage=null;
        try {
            TrackerServer trackerServer = trackerClient.getTrackerServer();
             storeStorage = trackerClient.getStoreStorage(trackerServer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "http://"+storeStorage
                 .getInetSocketAddress().getHostString()+":8888/";
    }



}
